﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FreedomERP.Services;
using FreedomERP.Services.Bd;
using FreedomERP.Services.Bd.Impl;
using FreedomERP.Models;
using Ext.Net.MVC;
using FreedomERP.Models.Bd;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class ProdCategoryController : Controller
    {
        IBdCategoryService categoryService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (categoryService == null) { categoryService = new BdCategoryServiceImpl(); }

            base.Initialize(requestContext);
        }
        //
        // GET: /ProdCategory/

        public ActionResult ProdCategoryIndex()
        {
            return View();
        }

        public ActionResult ProdCategoryIndexGeli()
        {
            return View();
        }


        public ActionResult ProdCategoryListPaging(String fName, String fRemark, int start, int limit)
        {
            int totalCount = 0;
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<BdCategory> result = categoryService.findModelCategories(fName, fRemark, start, limit, out totalCount, currAccountOrgunitFId);
            List<BdCategoryView> viewResult = new List<BdCategoryView>();
            foreach (var bdcate in result)
            {
                BdCategoryView cateView = new BdCategoryView();
                cateView.FID = bdcate.FId;
                cateView.FName = bdcate.FName;
                cateView.FReMark = bdcate.FRemark;
                viewResult.Add(cateView);
            }
            AjaxStoreResult accountPaging = new AjaxStoreResult(viewResult, totalCount);
            return accountPaging;
        }


        public ActionResult GetProdCategoryById(String fId)
        {
            if (fId != null)
            {
                BdCategory bdcate = categoryService.findModelCategoryById(fId);
                List<BdCategoryView> viewResult = new List<BdCategoryView>();
                BdCategoryView cateView = new BdCategoryView();
                cateView.FID = bdcate.FId;
                cateView.FName = bdcate.FName;
                cateView.FReMark = bdcate.FRemark;
                viewResult.Add(cateView);
                AjaxStoreResult accountPaging = new AjaxStoreResult(viewResult, viewResult.Count);
                return accountPaging;
            }
            else
            {
                AjaxFormResult response = new AjaxFormResult();
                response.Success = false;
                response.ExtraParams["msg"] = "所选规格无效！";
                return response;
            }
        }


        public ActionResult SaveProdCategory(String fid, String fName, String fRemark)
        {
            AjaxFormResult response = new AjaxFormResult();
            String returnFID = null;
            try
            {
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
                categoryService.saveOrUpdateModelCategory(BaseController.GetCurrLoginAccountFId(), currAccountOrgunitFId, fid, fName, fRemark, out returnFID);
            }
            catch (System.Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
                return response;
            }
            response.Success = true;
            response.ExtraParams["msg"] = returnFID;
            return response;
        }

        public ActionResult DeleteCategory(String fid)
        {
             AjaxResult response = new AjaxResult();
            try
            {
                categoryService.deleteModelCategory(fid);
            }
            catch (System.Exception ex)
            {

                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 产品单位管理页面
        /// <author>梁建东</author>
        /// </summary>
        public ActionResult ProdCategoryUnitIndex()
        {
            return View();
        }

        /// <summary>
        /// 产品单位分页列表
        /// <author>梁建东</author>
        /// </summary>
        public ActionResult ProdCategoryUnitListPaging(String fName, String fRemark, int start, int limit)
        {
            int totalCount = 0;
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
            List<BdCategory> result = categoryService.findUnitCategories(fName, fRemark, start, limit, out totalCount, currAccountOrgunitFId);
            List<BdCategoryView> viewResult = new List<BdCategoryView>();
            foreach (var bdcate in result)
            {
                BdCategoryView cateView = new BdCategoryView();
                cateView.FID = bdcate.FId;
                cateView.FName = bdcate.FName;
                cateView.FReMark = bdcate.FRemark;
                viewResult.Add(cateView);
            }
            AjaxStoreResult accountPaging = new AjaxStoreResult(viewResult, totalCount);
            return accountPaging;
        }

        /// <summary>
        /// 获取产品单位信息
        /// <author>梁建东</author>
        /// </summary>
        public ActionResult GetProdCategoryUnitById(String fId)
        {
            if (fId != null)
            {
                BdCategory bdcate = categoryService.findUnitCategoryById(fId);
                List<BdCategoryView> viewResult = new List<BdCategoryView>();
                BdCategoryView cateView = new BdCategoryView();
                cateView.FID = bdcate.FId;
                cateView.FName = bdcate.FName;
                cateView.FReMark = bdcate.FRemark;
                viewResult.Add(cateView);
                AjaxStoreResult accountPaging = new AjaxStoreResult(viewResult, viewResult.Count);
                return accountPaging;
            }
            else
            {
                AjaxFormResult response = new AjaxFormResult();
                response.Success = false;
                response.ExtraParams["msg"] = "所选单位无效！";
                return response;
            }
        }

        /// <summary>
        /// 保存产品单位信息
        /// <author>梁建东</author>
        /// </summary>
        public ActionResult SaveProdCategoryUnit(String fid, String fName, String fRemark)
        {
            AjaxFormResult response = new AjaxFormResult();
            String returnFID = null;
            try
            {
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
                categoryService.saveOrUpdateUnitCategory(BaseController.GetCurrLoginAccountFId(), currAccountOrgunitFId, fid, fName, fRemark, out returnFID);
            }
            catch (System.Exception ex)
            {
                response.Success = false;
                response.ExtraParams["msg"] = ex.Message;
                return response;
            }
            response.Success = true;
            response.ExtraParams["msg"] = returnFID;
            return response;
        }
    }
}
